package com.wink_172.activity

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.Environment
import android.os.IBinder
import android.util.Log
import android.view.View
import androidx.databinding.DataBindingUtil
import com.wink_172.commons.R
import com.wink_172.commons.databinding.ActivityMainBinding
import com.wink_172.library.LocalService
import com.wink_172.library.LocalService.MyBinder
import com.wink_172.library.activity.*
import com.wink_172.library.model.Constants
import com.wink_172.library.utils.*
import com.wink_172.library.utils.TencentCosUtils.uploadTectentFile
import org.xutils.common.Callback
import org.xutils.http.RequestParams
import org.xutils.x
import java.math.BigDecimal


class MainActivity : SmartActivity() {
    lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setStatusBarLightMode(resources.getColor(R.color.transparent80), R.color.white)  //自定义颜色
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        initView()
        StringListUtil.saveSingleString("123456", Constants.SEARCH_HISTORY)
//        Log.e(TAG, "StringList: ====>>" + StringListUtil.queryStringList(Constants.SEARCH_HISTORY)?.size)

        if (!checkPermissions(Constants.permissions)) {
            requestPermissions(Constants.ACTIVITY_REQUEST_CODE_PERMISSION, Constants.permissions)
        }

        bucket = Constants.BUCKET_TIANTIANTIAOWU;//请注意按需选择
//        bucket=Constants.BUCKET_WINK172;//请注意按需选择
    }

    var bucket = Constants.BUCKET_TIANTIANTIAOWU;

    fun initView() {
        binding!!.btnControlList.setOnClickListener(this)
        binding!!.dynamicLayout.setOnClickListener(this)
        binding!!.btnAnimList.setOnClickListener(this)
        binding!!.btnNumberList.setOnClickListener(this)
        binding!!.btnTestWeb.setOnClickListener(this)
        binding!!.btnUpload.setOnClickListener(this)
        binding!!.btnSelImages.setOnClickListener(this)
        binding!!.btnSelSingleImage.setOnClickListener(this)
        binding!!.btnSearchBar.setOnClickListener(this)

        binding!!.btnSwitchLang.setOnClickListener(this)
        binding!!.btnWeb.setOnClickListener(this)
        binding!!.btnDownload.setOnClickListener(this)
        binding!!.btnBlur.setOnClickListener(this)
        binding!!.btnFeedback.setOnClickListener(this)
        binding!!.btnTestActivity.setOnClickListener(this)
        binding!!.startService.setOnClickListener(this)
        binding!!.stopService.setOnClickListener(this)
        binding!!.bindService.setOnClickListener(this)
        binding!!.unbindService.setOnClickListener(this)
        binding!!.btn1.setOnClickListener(this)
        binding!!.btnDownloadList.setOnClickListener(this)
    }



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_OK) {
            return
        }
        when (requestCode) {
            Constants.ACTIVITY_REQUEST_CODE3001 -> {    //0简体    1英文    2繁体
                var pos: Int = data!!.getIntExtra(Constants.PARAM_DATA1, 0)

            }
        }
    }


    override fun onClick(v: View) {
        super.onClick(v)
        when (v.id) {
            R.id.btn_download_list -> {
                DownloadListActivity.startActivity(activity, 0)
            }

            R.id.btn1 -> {
                val save_path = CommonJavaUtils.getCachePath(x.app()) + "bbc.apk"
                DownAppActivity.startActivity(
                    activity,
                    "https://fun4demo-1254407438.cos.ap-guangzhou.myqcloud.com/temp_fd/BiBu_v5.3.apk",
                    save_path, "", "", ""
                )
            }

            R.id.dynamic_layout -> {
                DynamicLayoutActivity.startActivity(this)
            }

            R.id.btn_test_web -> {
                TestWebActivity.startActivity(this)
            }

            R.id.btn_number_list -> {
                DecimalFormatActivity.startActivity(activity)
            }

            R.id.btn_anim_list -> {
                AnimActivity.startActivity(activity)
            }

            R.id.btn_upload -> {
                UploadActivity.startActivity(this)
            }

            R.id.btn_test_activity -> {


                TestActivity.startActivity(this)
            }

            R.id.btn_feedback -> FeedBackActivity.startActivity(this)
            R.id.btn_blur -> BlurActivity.startActivity(this)
            R.id.btn_switch_lang -> LanguageActivity.startActivity(
                this,
                Constants.ACTIVITY_REQUEST_CODE3001
            )

            R.id.btn_web ->
            {
//              WebActivity.startActivity(this, "标题", "https://www.baidu.com/")
                AwardWebActivity.startActivity(activity,0)
            }

            R.id.btn_download -> DownAppActivity.startActivity(this, Constants.APP_WEB_URL)
            R.id.btn_search_bar -> SearchBarActivity.startActivity(this)
            R.id.btn_sel_single_image -> SelectorSingleImageActivity.startActivity(this)
            R.id.btn_sel_images -> SelectorMultiImageActivity.startActivity(this)
            R.id.btn_control_list -> ControlActivity.startActivity(this)
            R.id.start_service -> {
                val startIntent = Intent(this, LocalService::class.java)
                startService(startIntent)
            }

            R.id.stop_service -> {
                val stopIntent = Intent(this, LocalService::class.java)
                isBound = false
                stopService(stopIntent)
            }

            R.id.bind_service -> {
                // android:process=":remote"   error
                //进程之间不能直接传递对象，传递的是该Binder对象的映射（代理对象）
                val bindIntent = Intent(this, LocalService::class.java)
                isBound = bindService(bindIntent, callback, BIND_AUTO_CREATE)
            }

            R.id.unbind_service -> {
                Log.e(TAG, "click Unbind Service button")
                if (isBound) {
                    unbindService(callback)
                    isBound = false
                }
            }
        }
    }

    private var myBinder: MyBinder? = null
    private var isBound = false

    /**
     * callback
     * */
    private val callback: ServiceConnection = object : ServiceConnection {
        override fun onServiceDisconnected(name: ComponentName) {}
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            myBinder = service as MyBinder
            myBinder!!.mCallback = object :
                com.wink_172.library.callback.ICallback {
                override fun onSendEvent(
                    event: Int,
                    vararg args: Any?
                ) {//这个callback用的非常巧妙    //这里因为不能传递非Serialiazable,对象 所以需要精简代码

                }
            }
            myBinder!!.startDownload()
        }
    }

    companion object {
        fun startActivity(context: Context, vararg args: Any?) {
            val intent = Intent(context, MainActivity::class.java)

            //intent.putExtra(Constants.PARAM_DATA1, (String) args[0]);
            context.startActivity(intent)
        }

        private const val TAG = "MainActivity"
    }
}